//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    hiddens: true,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    classifyProduct0: [],
    classifyProduct1: [],
    classifyProduct2: [],
    classifyProduct3: [],
    classifyProduct4: [],
    classifyProduct5: [],
    classifyProduct6: [],
    classifyBanner0: [],
    classifyBanner1: [],
    classifyBanner2: [],
    classifyBanner3: [],
    classifyBanner4: [],
    classifyBanner5: [],
    classifyBanner6: [],
    tips: ["您的今天的加入将会是明天的成功", "欢迎成为我们的酒庄庄主！"],
    tabMenus: [{
      title: "全部商品",
      logo: "../../image/quanbu@2x.png",
      id: 1
    },
    {
      title: "品德共享",
      logo: "../../image/pindegongxiang@2x.png",
      id: 2
    },
    {
      title: "成为庄主",
      logo: "../../image/chengwei@2x.png",
      id: 3
    },
    {
      title: "余额",
      logo: "../../image/yue@2x.png",
      id: 4
    },
    ],

  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    getIndexBanner(that)
    app.showShareMenus();
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      app.globalData.referrerId = scene;
      app.getUserInfo();
    }
    if (options.id) {
      app.globalData.referrerId = options.id;
      app.getUserInfo();
    } else {
      app.getUserInfo();
    }
    getDirectProduct(this, 1);
    getDirectProduct(this, 2);
    getDirectProduct(this, 3);
    getDirectProduct(this, 4);
    getDirectProduct(this, 5);
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  navMenus: function (e) {
    if (e.target.id == 1) {
      wx.navigateTo({
        url: '../../pages/allGoods/allGoods',
      })
    } else if (e.target.id == 4) {
      wx.navigateTo({
        url: '../../pages/page4/page4',
      })
    } else if (e.target.id == 3) {
      wx.navigateTo({
        url: '../../pages/becomeOwner/becomeOwner',
      })
    } else if (e.target.id == 2) {
      wx.navigateTo({
        url: '../../pages/aboutUs/aboutUs',
      })
    }
  }

  , onShow: function () {
    getPersonLocation(this)
  },
  onShareAppMessage: function (res) {
    return {
      title: '正宗文山三七',
      path: '/pages/direct/direct?id=' + wx.getStorageSync("userid"),
    }
  }
})


function getDirectProduct(that, id) {
  var _classifyBanner = [];
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
    //  directId: 1,
      pageNum: 1,
      pageCount: 30,
      classify: id,
      putaway: 1
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
   //   console.log(res)
     if(id==1){
       that.setData({
         classifyProduct0: res.data
       })
     } else if (id == 2) {
       that.setData({
         classifyProduct1: res.data
       })
     } else if (id == 3) {
       that.setData({
         classifyProduct2: res.data
       })
     } else if (id == 4) {
       that.setData({
         classifyProduct3: res.data
       })
     } else if (id == 5) {
       that.setData({
         classifyProduct4: res.data
       })
     }
       
      setLists(that);
    }
  })
}

function setLists(that) {
  console.log()
  that.setData({
    wineList: [
      {
        name1: "三七原料",
        name2: "Raw Materials",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/sanqitou@2x.png",
        products: that.data.classifyProduct0,
        classifyBanner: that.data.classifyBanner0,
        id: 8
      },
      {
        name1: "三七系列药品 ",
        name2: "Series Of Drugs",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/sanqifen@2x.png",
        products: that.data.classifyProduct1,
        classifyBanner: that.data.classifyBanner1
      },
      {
        name1: "三七保健品 ",
        name2: "Health Care Products",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/yaoping@2x.png",
        products: that.data.classifyProduct2,
        classifyBanner: that.data.classifyBanner2
      },
      {
        name1: "三七健康食品 ",
        name2: "Healthy Food",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/yaoping@2x.png",
        products: that.data.classifyProduct3,
        classifyBanner: that.data.classifyBanner3
      },
      {
        name1: "三七日化产品系列 ",
        name2: "Daily Product Series",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/yaoping@2x.png",
        products: that.data.classifyProduct4,
        classifyBanner: that.data.classifyBanner4
      },
    /*  {
        name1: "儿童葡萄汁 ",
        name2: "Kids Bio Grape Juice",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/Kids@2x.png",
        products: that.data.classifyProduct5,
        classifyBanner: that.data.classifyBanner5
      },
      {
        name1: "其他 ",
        name2: "Others",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "",
        products: that.data.classifyProduct6,
        classifyBanner: that.data.classifyBanner6
      },*/
    ]
  })
}

function getIndexBanner(that) {
  wx.request({
    url: app.globalData.allUrl + "/utilAction/selectIndexBanner",
    data: {
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     // console.log(res.data)
      that.setData({
        imgUrls: res.data
      })
    }
  })
}

function getPersonLocation(thisid) {//
  var that = thisid;
  wx.getLocation({
    success: function (res) {
      wx.request({
        url: app.globalData.allUrl + "/utilAction/getmapinfo",
        data: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: function (ops) {
          wx.setStorageSync('checekgetPersonLocation', 1)
          if (that.data.noRoot) {
            that.setData({
              hasRoot: false,
              noRoot: true
            })
          }
          //console.log(ops.data)
          var newJson = ops.data.indexOf("{")
          var nextJson = ops.data.lastIndexOf(")")
          var userJson = ops.data.substring(newJson, nextJson)
          var res = JSON.parse(userJson)
          var addressDetaile = res.result.addressComponent.province + res.result.addressComponent.city + res.result.addressComponent.district
          that.setData({
            district: res.result.addressComponent.district,
            adcode: res.result.addressComponent.adcode,
          })
          app.addressDetailes = addressDetaile;
          app.district = res.result.addressComponent.district
          app.adcode = res.result.addressComponent.adcode;
          wx.setStorageSync('adcode', res.result.addressComponent.adcode)
          // console.log(app.addressDetailes)
          //  isAgent(that)//判断该区域有无代理商
          // requestSpikr(that, res.result.addressComponent.adcode)
        }
      })
    }, fail: function () {
      wx.setStorageSync('checekgetPersonLocation', 0)
      that.setData({
        hasRoot: true,
        noRoot: false
      })
    }
  })

}